package com.taa.controller;

import com.taa.dto.PaymentDTO;
import com.taa.enums.ResponseCode;
import com.taa.exception.AppException;
import com.taa.response.Response;
import com.taa.service.IPaymentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: taa
 * @Desc: 支付控制层
 * @Create: 2025-06-22 19:21
 **/
@Slf4j
@RestController
@RequestMapping("/payment") // 对应网关转发路径 /api/payment/**
public class PaymentController {

    @Resource
    private IPaymentService paymentService;

    /**
     * 模拟支付接口
     * 在真实场景中，这个接口可能是由order-service在订单创建后调用的
     * 这里我们简化为前端直接调用
     * @param paymentDTO 包含订单号和金额
     * @return Response<Boolean>
     */
    @PostMapping("/pay")
    public Response<Boolean> mockPay(@RequestBody PaymentDTO paymentDTO) {
        try {
            log.info("接收到支付请求, orderNo: {}", paymentDTO.getOrderNo());
            Boolean success = paymentService.processPayment(paymentDTO);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(success)
                    .build();
        } catch (AppException e) {
            log.error("支付处理业务异常, orderNo: {}", paymentDTO.getOrderNo(), e);
            return Response.<Boolean>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("支付处理系统异常, orderNo: {}", paymentDTO.getOrderNo(), e);
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}

